#input thread_id,and view its posts

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from BeautifulSoup import BeautifulSoup
from xmw.tianya.bot import *

from xmw import tianya

from xmw import error
import os
import urllib2, urllib
import uuid

from google.appengine.ext import webapp

class MainPage(xmw.RequestHandler):
    def get(self):
        if  not self.request.GET.has_key('thread_id'):
            e = error.ARGUMENT_FAULT("URI:list_thread_post needs parameter thread_id.    /portal/list_thread_post?thread_id=XXXX&offset=XX")
            self.render_error(e)
            return
        
        thread_id = self.request.GET['thread_id']
        
        thread_q=Bot_Thread.all()
        thread_q.filter('thread_id =',thread_id)
        thread=thread_q.get()
        
        if thread is None:
            e   =error.THREAD_NOT_EXIST("thread_id:%s"%thread_id)
            self.render_error(e)
            return
        
        # now we have a valid thread object
        offset=0
        if self.request.GET.has_key('offset'):
            offset  = self.request.GET['offset']
        offset=int(offset)
        
        if offset > thread.offset:
            e=error.OFFSET_OUT_OF_RANGE("requested offset:%d,current thread offset:%d and page count:%d"%(offset,thread.offset,thread.page_cnt))
            self.render_error(e)
            return
        
        lst_post_q  =   Post.all()
        lst_post_q.filter("thread_id =" , thread_id)
        lst_post_q.filter("offset =",offset)
        
        lst_post    =lst_post_q.fetch(1000)
        index =1
        for post in lst_post:
            if index %2 ==1:
                post.line_class="odd"
            else:
                post.line_class="even"
            
            index=index+1
        
        lst_pager=[]
        for i in range(thread.offset):
            if i != offset:
                pager="<a  class=\"ae-page-selected ae-page-number\" href=\"list_thread_post?thread_id=%s&offset=%d\">%d</a>"%(thread.thread_id,i,i+1)
            else:
                pager="%d"%(i+1)
            
            lst_pager.append(pager)    
                
        #print lst_post
        template_values = {
                               "thread":thread,\
                               "post_cnt":len(lst_post),\
                               "lst_post":lst_post,\
                               "lst_pager":lst_pager,\
                               "offset":str(offset)
                        }
        self.render_template(template_values, 'list_thread_post')        
        
        return 
        

def main():
    application = webapp.WSGIApplication(
                                       [('/portal/list_thread_post', MainPage)],
                                       debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
